require 'parsers/excel_reader'

class Cartasur::CartAdmin < Cartasur::CartasurModel

  def self.send_admin_email(admin)

    contributing_users = Cartasur::User.where(" last_activity_at is not null  ").order(" last_activity_at desc ")

    info = contributing_users.map{|user|
      visiteds = Cartasur::Visited.where("user_id = #{user.id}").where("created_at > '#{1.week.ago.to_s(:db)}'").all
      measure_info = visiteds.map{|visited|
          track = Cartasur::Track.find_by_id(visited.track_id)
          if track.blank?
            msize = 0
          else
            msize = track.measures.try(:size)
          end
          loc = Cartasur::Location.find(visited.protocol.objective.location).try(:name)
          {measures: msize, location: loc}
      }

      {user: user.login, protocols: visiteds.try(:size), info: measure_info }
    }
    # sort them
    info = info.sort_by {|elt| elt[:user].split("asur").map(&:to_i).inject(&:+)}

    CartMailer.delay.deliver_admin_email(admin, info) if admin.email

    #also send to brussense
    #vub_user = Cartasur::User.where(:login => "cartasur66").first
    #CartMailer.delay.deliver_admin_email(vub_user, info)

  end

  #already done, so removed the self.create_users
  def self.create_users(start=66,endno=66)

    i = start

    while i <= endno

      u = Cartasur::User.new(
         {  login: "cartasur"+i.to_s,
            email: 'cartasur'+i.to_s+'@noisetube.net'
          })
      u.city_id = 1
      u.phonemodel = "HTC One X"
      u.location = "Paris, FR"
      u.public = true
      u.password =  "cartASUR"+i.to_s
      u.password_confirmation = u.password

      u.encrypt_password

      u.save!

      i = i + 1
     end

    logger.info "Created the cartASUR users"
  end


  def self.setup_admin_accs
  #get admin user
  admin = Cartasur::User.where("login like '%administr%'").first

  #get all the objectives
  unless admin.nil?
    allobjs = Cartasur::Objective.all
    allobjs.each { |o|
      #save as new protocol
      p = Cartasur::Protocol.new
      p.users_id = admin.id
      p.objective_id = o.id
      p.save
    }
  end


  end

  #@param users - array of user ids
  #@param userprofile - the userid to use for the protocols
  def self.setup_test_accs(users, userprofile=nil)

    if userprofile.nil?
      allobjs =  Cartasur::Objective.all
    else
      user_of_profile = Cartasur::User.where("login like '%#{userprofile}%'").first
      allobjs = user_of_profile.objectives
    end

    users.each { |num|
      cart = Cartasur::User.where("login like 'cartasur#{num}%'").first
      #get all the objectives
      unless cart.nil?

        allobjs.each { |o|
          #save as new protocol
          p = Cartasur::Protocol.new
          p.users_id = cart.id
          p.objective_id = o.id
          p.save
          puts p.id.to_s << " | users_id: "<< p.users_id.to_s << " objectives id: "<< p.objective_id.to_s
        }
      end
      }
  end

  #done
  def self.update_winter_objectives
    #get all objectives
    allobjs =  Cartasur::Objective.all

    #update their months
    allobjs.each { |objective|
      objective.months = "4,5,6,7,8,9"
      #save this one
      objective.save

      #create another one
      new_objective = Cartasur::Objective.new
      new_objective.time_range = objective.time_range
      new_objective.days = objective.days
      #different months
      new_objective.months = "10,11,12,1,2,3"
      new_objective.location = objective.location
      #save
      new_objective.save

      #assign same users (protocols) to this new objective
      protocols = objective.protocols

      protocols.each { |protocol|
        new_protocol = Cartasur::Protocol.new
        new_protocol.user = protocol.user
        new_protocol.objective = new_objective
        new_protocol.save
      }
    }

  end


  # parse questionnaire excel
  def self.process_questionnaire_records
    ExcelReader.read_cartasur_questionnaire
    puts "Processed successfully!! Exiting.."
  end
end